package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.ChannelMapper;
import com.heima.admin.service.ChannelService;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.admin.dtos.ChannelDTO;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ChannelServiceImpl extends ServiceImpl<ChannelMapper, AdChannel> implements ChannelService {

    /**
     * 分页查询
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findListByQuery(ChannelDTO dto) {
        //1.校验业务参数
        //检查分页参数
        dto.checkParam();
        //2.执行分页条件查询
        //2.1分页条件对象
        Page<AdChannel> pageReq = new Page<>(dto.getPage(), dto.getSize());
        //2.2查询条件
        LambdaQueryWrapper<AdChannel> wrapper = Wrappers.<AdChannel>lambdaQuery();
        //判断前端是否传name  如果有值   添加模糊条件查询
        if(StringUtils.isNotBlank(dto.getName())){
            wrapper.like(AdChannel::getName,dto.getName());
        }
        //判断前端状态是否为空
        if(dto.getStatus()!=null){
           wrapper.eq(AdChannel::getStatus,dto.getStatus());
        }
        //序号升序
        wrapper.orderByAsc(AdChannel::getOrd);
        //执行查询
        IPage<AdChannel> page = page(pageReq, wrapper);
        //封装响应结果
        PageResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), page.getTotal(), page.getRecords());
        return result;
    }

    /**
     * 新增频道
     * @param adChannel
     * @return
     */
    @Override
    public ResponseResult insert(AdChannel adChannel) {
        //1.参数校验
        //判断名字是否为空
        String name = adChannel.getName();
        if (StringUtils.isBlank(name)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道参数不能为空");
        }
        //判断名称是否大于10字符
        if(name.length()>10){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道名称不能大于10个字符");
        }
        //判断数据名称是否已存在
        int count = this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName, name));
        if(count>0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道已存在");
        }
        //2.保存到数据库
        adChannel.setCreatedTime(new Date());
        this.save(adChannel);
        //3.返回封装结果
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 修改频道
     * @param adChannel
     * @return
     */
    @Override
    public ResponseResult update(AdChannel adChannel) {
        //1.参数校验
        //判断id是否为空
        if(adChannel.getId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道id为空");
        }
        AdChannel oldChannel = this.getById(adChannel.getId());
        String newname = adChannel.getName();
        if(StringUtils.isNotBlank(newname)&&!newname.equals(oldChannel.getName())){
            //判断名称是否大于10字符
            if(newname.length()>10){
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道名称不能大于10个字符");
            }
            //判断数据名称是否已存在
            int count = this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName, newname));
            if(count>0){
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道已存在");
            }
        }
        //2.根据id修改数据
        this.updateById(adChannel);
        //3.返回封装结果
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 根据id删除频道
     * @param id
     * @return
     */
    @Override
    public ResponseResult delete(Integer id) {
        //参数校验（id 检查对应的数据 ，状态是否为TRUE）
        if(id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"id不能为空");
        }
        AdChannel adChannel = this.getById(id);
        if(adChannel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"对应的频道信息不存在");
        }
        //判断频道是否有效
        if(adChannel.getStatus()){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道有效，不允许删除");
        }
        //删除频道
        this.removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
